package com.jdf.es;

import com.jdf.util.ESClientUtil;
import com.sun.org.apache.xpath.internal.operations.Gte;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;

public class EsTest {
    @Test
    public void testAdd() throws Exception{
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        HashMap<String, Object> map = new HashMap<>();
        // 2.准备要放入到ES中的数据
        for (int i = 1; i < 100; i++) {
            map.put("name",i%2==0?"法外狂徒":"张三");
            map.put("age",i);
            map.put("id",i);
            map.put("sex",i%2);
            // 3.使用链式编程，将数据存入到ES中
            IndexRequestBuilder builder = client.prepareIndex("pethome", "pet", ""+i);
            IndexRequestBuilder builder1 = builder.setSource(map);
            System.out.println(builder1.get());
        }
        /*HashMap<String, Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("age",2);
        map.put("id",1);
        map.put("sex",1);
        // 3.使用链式编程，将数据存入到ES中
        IndexRequestBuilder builder = client.prepareIndex("pethome", "pet", "1");
        IndexRequestBuilder builder1 = builder.setSource(map);
        // 4.得到添加文档结果
        */
        // 5.关流
        client.close();

    }
    @Test
    public void testGet() throws Exception{
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.查询
        GetResponse response = client.prepareGet("pethome", "pet", "3").get();
        // 3.打印输出
        // System.out.println(response);
        System.out.println(response.getSource());
        // 4.关流
        client.close();

    }
    @Test
    public void testUpdate() throws Exception{
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.准备要放入到ES中的数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","发哥");
        // 3.使用链式编程，将数据存入到ES中
        UpdateResponse response = client.prepareUpdate("pethome", "pet", "1").setDoc(map).get();
        // 4.得到修改结果
        System.out.println(response);
        // 5.关流
        client.close();

    }
    @Test
    public void testDelte() throws Exception{
        // 1.获取链接
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.删除
        DeleteResponse response = client.prepareDelete("pethome", "pet", "1").get();
        // 3.关流
        client.close();
    }

    // 查询条件：- 查询用户表，name包含：我在源码，age在1~12之间，每页大小2，从二页开始查，按照age倒序

    @Test
    public void testComplex() throws Exception{
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.得到搜索对象
        SearchRequestBuilder pethome = client.prepareSearch("pethome");
        // 3.指定要搜索的类型
        pethome.setTypes("pet");
        // 4.指定Query搜索对象，当参数是接口时我们可以传递：接口的工具类、接口实现、匿名内部类，此处传递接口工具类
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        // name
        boolQuery.filter(QueryBuilders.rangeQuery("age").gte(1).lte(20)).filter(QueryBuilders.termQuery("sex",1));
        boolQuery.must(QueryBuilders.matchQuery("name","张三"));
        // 放入搜索条件
        pethome.setQuery(boolQuery);
        // 页码
        pethome.setFrom(5);
        // 页数
        pethome.setSize(5);
        // 倒序列
        pethome.addSort("age", SortOrder.DESC);
        // 打印
        SearchHits hits = pethome.get().getHits();
        for (SearchHit doc: hits) {
            System.out.println(doc.getSourceAsMap());
        }
        // 关流
        client.close();
    }
}
